package com.hdoit.kylin.web.groupActivity.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.baseInfo.model.Term;
import com.hdoit.kylin.logical.baseInfo.service.ITermService;
import com.hdoit.kylin.logical.groupActivity.model.Schedule;
import com.hdoit.kylin.logical.groupActivity.service.IScheduleService;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.groupActivity.ScheduleSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;
import com.hdoit.kylin.web.common.ScheduleUtils;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

/**
 * @author lufu
 * @email
 * @createtime 2017年08月03日 10:10:25
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/groupActivity/schedule/")
public class ScheduleController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/groupActivity/schedule/";
	}

	private IScheduleService scheduleService;

	public IScheduleService getScheduleService() {
		return scheduleService;
	}

	@Resource
	public void setScheduleService(IScheduleService scheduleService) {
		this.scheduleService = scheduleService;
	}

	private IStudentService studentService;

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	private ITeacherService teacherService;

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "scheduleDetailed";
	private String termId;

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			ScheduleSearch search, boolean ajaxPost, HttpServletRequest request) {
		Schedule schedule;
		List<Schedule> schedules = scheduleService.queryList(search,pagerModel);
		if(schedules != null && schedules.size()>0){
			schedule = schedules.get(0);
		}else {
			schedule = new Schedule();
		}
		
		Term term;
		ITermService termService = CommonApplicationContextUtil.getCurrentContext().getBean(ITermService.class);
		if(search.getTermId()!= null){
			term = termService.queryModel(search.getTermId());
			
		}else{
			term = termService.queryList().get(0);
		}
		termId = term.getTermId();
		term.generateDateList();
		System.err.println(term.getWeekDates().size());
		ScheduleUtils.setWeekDate(term.getWeekDates());
		
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("schedule", schedule);
		model.addAttribute("scheduleSearch", search);
		model.addAttribute("scheduleItems", ScheduleUtils.schedules);
		
		if (ajaxPost) {
			return view(outputViewName);
		}

		return view(indexViewName);
	}
	
	@RequestMapping(value="save", method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model, Schedule schedule, PagerModel pagerModel, ScheduleSearch search, boolean ajaxPost){
		System.err.println("ScheduleSave");
		if(StringUtils.isEmpty(schedule.getId())){
			schedule.setId(CommonUtils.getKeyId());
			schedule.setCreateDate(DateUtils.getCurrent());
			schedule.setCreateId(getLoginModel().getLoginId());
			schedule.setModelStatus(ModelStatus.add);
			schedule.setTermId(termId);
		}else{
			schedule.setModelStatus(ModelStatus.update);
		}
		
		ActionResult result;
		if(scheduleService.applyChange(schedule)){
			result = new ActionResult();
		}else {
			result = new ErrorResult();
		}
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("scheduleSearch", search);
		model.addAttribute("scheduleItems", ScheduleUtils.schedules);	
		model.addAttribute("schedule",schedule);
		model.addAttribute(ManageContainer.RESULTKEY,result);
		
		return view(indexViewName);
	}
}